package problems;

import static lib.MathLib.isPrime;

public class Euler035 extends AbstractEuler {

	@Override
	public Number calculate() {
		int numberOfCircularPrimes = 0;
		
		for (int i = 2; i < 1000000; i++) {
			if (String.valueOf(i).contains("0")) continue;
			if (isCircularPrime(i)) numberOfCircularPrimes++;
		}
		
		return numberOfCircularPrimes;
	}
	
	private boolean isCircularPrime(int i) {
		if (!isPrime(i)) return false;
		StringBuffer number = new StringBuffer(String.valueOf(i));
		for (int rotation = 0; rotation < number.length(); rotation++) {
			//rotate the number by moving the first digit to the back
			number.append(number.charAt(0));
			number.deleteCharAt(0);
			
			//the result should also be prime
			if (!isPrime(new Integer(number.toString()))) return false;
		}
		
		//if all rotations were prime, then it was a circular prime
		return true;
	}

	@Override
	protected Number getCorrectAnswer() {
		return 55;
	}

}
